package com.github.tonydeng.fmj.pool;

import com.github.tonydeng.fmj.runner.ChiaCommandRunner;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;
import java.util.UUID;

public class ThreadPoolTask implements Runnable {

    private Logger logger = Logger.getLogger(ThreadPoolTask.class);

    private ThreadPoolOps poolOps;
    private ChiaCommandRunner chiaCommandRunner;

    private String workId;
    private String size;
    private String num;
    private String bytes;
    private String theadNum;
    private String buckets;
    private String farmerKey;
    private String poolKey;
    private String tmpDir;
    private String tmp2Dir;
    private String finalDir;
    private String dCount;

    public ThreadPoolTask(Map<String, String> values, ThreadPoolOps poolOps, ChiaCommandRunner chiaCommandRunner) {
        this.workId = values.get("workId");
        this.size = values.get("size");
        this.num = values.get("num");
        this.bytes = values.get("bytes");
        this.theadNum = values.get("theadNum");
        this.buckets = values.get("buckets");
        this.farmerKey = values.get("farmerKey");
        this.poolKey = values.get("poolKey");
        this.tmpDir = values.get("tmpDir");
        this.tmp2Dir = values.get("tmp2Dir");
        this.finalDir = values.get("finalDir");
        this.dCount = values.get("dCount");
        this.poolOps = poolOps;
        this.chiaCommandRunner = chiaCommandRunner;
    }

    @Override
    public void run() {
        String id = UUID.randomUUID().toString().replace("-", "");
        try {
            //记录p盘任务id
            poolOps.workListIn(id, workId, size, num, bytes, theadNum, buckets, farmerKey, poolKey, tmpDir, tmp2Dir, finalDir, dCount);

            //开始p盘任务
            chiaCommandRunner.chiaPlots(workId, size, num, bytes, theadNum, buckets, farmerKey, poolKey, tmpDir, tmp2Dir, finalDir, dCount);

            //p盘完成的任务列表
            poolOps.finishListIn(id, workId, size, num, bytes, theadNum, buckets, farmerKey, poolKey, tmpDir, tmp2Dir, finalDir, dCount);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}
